﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Account.Model;
using Yipusoft.Common.Model;
using System.Drawing;

public partial class Combat_InputScore : System.Web.UI.Page
{
    TextBox txtCount;
    RadioButtonList rdioList;
    private Control _survey = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }
    private void Bind()
    {
        lblTitle.Text = "待录入士兵";
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        if (user == null)
            return;
        Soldier soldier = new Soldier();       
        List<SoldierArchiveInfo> soldierList = soldier.GetSoldiersByWhere(" DepartmentID='" + user.DepartmentID + "' And IsDelete='0'", "SoldierID", "Desc", 1000, 0);
        List<TaskItemsInfo> taskitemList = TaskItems.GetList(" TaskID='" + Request["TaskID"] + "'");
        List<SoldierArchiveInfo> dataList = new List<SoldierArchiveInfo>();
        foreach (SoldierArchiveInfo info in soldierList)
        {
            SoldierRankInfo rankInfo = SoldierRank.GetSoldierRank(info.SoldierID);            
            if (rankInfo != null)
            {
                List<SoldierStationInfo> stationList = SoldierStation.GetList("  SoldierID='" + info.SoldierID + "'");
                foreach (SoldierStationInfo s in stationList)
                {
                    if (taskitemList.Where(o => o.StationID == s.StationID && o.RankID == rankInfo.RankID).Count() > 0)
                    {
                        if (dataList.Where(o => o.SoldierID == info.SoldierID).Count() <= 0)
                        {
                            dataList.Add(info);
                        }

                    }
                }
            }
        }
        dltSoldiers.DataSource = dataList;
        dltSoldiers.DataBind();
    }
    protected void lnbtnSoldier_Command(object sender, CommandEventArgs e)
    {
        lblTitle2.Text = "【" + ((LinkButton)sender).Text + "】 成绩录入";
        Soldier soldier = new Soldier();
        SoldierArchiveInfo info = soldier.GetSoldierInfoByID(e.CommandArgument.ToString());
        if (info == null)
            return;
        lblRank.Text = "";
        SoldierRankInfo rankInfo = SoldierRank.GetSoldierRank(info.SoldierID);
        lblRank.Text = Yipusoft.Common.BLL.CodeItem.GetCodeItem(rankInfo.RankID).ItemName;
        List<CodeItemInfo> itemList= Yipusoft.Common.BLL.CodeItem.GetList("ItemID IN (Select StationID From SoldierStation Where SoldierID='" + info.SoldierID + "')");
        lblStation.Text = "";
        foreach (CodeItemInfo code in itemList)
        {
            lblStation.Text += code.ItemName+"、";
        }
        btnSave.CommandArgument = e.CommandArgument.ToString();
        dltSkill.DataSource = TaskSkill.GetList(" SkillID IN(Select SkillID From TaskSubject Where  TaskID='" + Request["TaskID"] + "' And TaskItemID IN ( Select ID From TaskItems Where TaskID ='" + Request["TaskID"] + "' And RankID='" + rankInfo.RankID + "' And StationID IN (Select StationID From SoldierStation Where SoldierID='" + info.SoldierID + "')))");
        dltSkill.DataBind();
        
    }
    protected void dltSkill_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        Label lblSkillID = e.Item.FindControl("lblSkill") as Label;
        Label lblType = e.Item.FindControl("lblSkillType") as Label;
        Soldier soldier = new Soldier();
        SoldierArchiveInfo info = soldier.GetSoldierInfoByID(btnSave.CommandArgument);

        TaskScoreInfo scoreInfo=null;
        if (info != null)
        {
            scoreInfo = TaskScore.GetList(" TaskID='" + Request["TaskID"] + "' And DepartmentID='" + info.DepartmentID + "' And SoldierID='" + info.SoldierID + "' And SkillID='" + lblSkillID.Text + "'").FirstOrDefault();
        }


        if (lblType.Text.Equals("3"))
        {
            TextBox txt = e.Item.FindControl("txtCount") as TextBox;
            txt.Visible = true;
            if (info != null)
            {

                if (scoreInfo != null)
                {
                    txt.Text = scoreInfo.Score;
                }
            }
        }
        else if (lblType.Text.Equals("1"))
        {
            RadioButtonList ralist = e.Item.FindControl("rbtn") as RadioButtonList;
            ralist.Visible = true;
            if (scoreInfo != null)
            {
                ralist.SelectedValue = scoreInfo.Score;
            }

        }
        else if (lblType.Text.Equals("2"))
        {
            DropDownList ddlHour = e.Item.FindControl("ddlHour") as DropDownList;
            ddlHour.Visible = true;
            DropDownList ddlMinus = e.Item.FindControl("ddlMinus") as DropDownList;
            ddlMinus.Visible = true;
            DropDownList ddlSec = e.Item.FindControl("ddlSec") as DropDownList;
            ddlSec.Visible = true;
            DropDownList ddlMinsec = e.Item.FindControl("ddlMinsec") as DropDownList;
            ddlMinsec.Visible = true;

            for (int i = 0; i <= 12; i++)
            {
                ddlHour.Items.Add(new ListItem(i.ToString() + "小时", i.ToString()));
            }
            for (int i = 0; i < 60; i++)
            {
                ddlMinus.Items.Add(new ListItem(i.ToString() + "分", i.ToString()));
            }
            for (int i = 0; i < 60; i++)
            {
                ddlSec.Items.Add(new ListItem(i.ToString() + "秒", i.ToString()));
            }
            for (int i = 0; i < 1000; i++)
            {
                ddlMinsec.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
            if (scoreInfo != null)
            {
                DateTime dt = DateTime.Parse(scoreInfo.Score);
                ddlHour.SelectedValue = int.Parse(dt.ToString("HH")).ToString();
                ddlMinus.SelectedValue = int.Parse(dt.ToString("mm")).ToString();
                ddlSec.SelectedValue = int.Parse(dt.ToString("ss")).ToString();
                ddlMinsec.SelectedValue = int.Parse(dt.ToString("fff")).ToString();
            }

        }
        //_survey = Page.LoadControl("MyTime.ascx");
        //_survey.ID = "myt";
        //Type t = _survey.GetType();
        //System.Reflection.MethodInfo m = t.GetMethod("Bind");
        //m.Invoke(_survey, new object[0]);
        //pn.Controls.Add(_survey);

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        TaskBaseInfo taskInfo= TaskBase.GetTaskBase(new Guid(Request["TaskID"].ToString()));
        //if (taskInfo.EndDate < DateTime.Now)
        //{
         //   lblPrompt.Text = "此任务已过期！";
         //   return;
        //}
        if (string.IsNullOrEmpty(btnSave.CommandArgument))
            return;
        Soldier soldier = new Soldier();
        SoldierArchiveInfo info = soldier.GetSoldierInfoByID(btnSave.CommandArgument);
        if (info == null)
            return;


        //List<TaskItemsInfo> taskitemList = TaskItems.GetList(" TaskID='" + Request["TaskID"] + "' And RankID='"+info.Rank+"' And StationID IN (Select StationID From SoldierStation Where SoldierID='"+info.SoldierID+"')");
        foreach (DataListItem item in dltSkill.Items)
        {
            //技能ID
            string skillID = ((Label)item.FindControl("lblSkill")).Text;

            //技能类型
            string skillType = ((Label)item.FindControl("lblSkillType")).Text;

            SoldierRankInfo rankInfo = SoldierRank.GetSoldierRank(info.SoldierID);

            List<TaskSubjectInfo> subList = TaskSubject.GetList(" TaskItemID IN (Select ID From TaskItems Where TaskID='" + Request["TaskID"] + "' And RankID='" + rankInfo.RankID + "' And StationID IN (Select StationID From SoldierStation Where SoldierID='" + info.SoldierID + "') ) And SkillID='" + skillID + "'");
            foreach (TaskSubjectInfo subinfo in subList)
            {
                string sc = string.Empty;
                if (skillType.Equals("2"))
                {
                    DropDownList ddlHour = item.FindControl("ddlHour") as DropDownList;
                    DropDownList ddlMinus = item.FindControl("ddlMinus") as DropDownList;
                    DropDownList ddlSec = item.FindControl("ddlSec") as DropDownList;
                    DropDownList ddlMinsec = item.FindControl("ddlMinsec") as DropDownList;
                    sc = new DateTime(2010, 8, 8, int.Parse(ddlHour.SelectedValue), int.Parse(ddlMinus.SelectedValue), int.Parse(ddlSec.SelectedValue), int.Parse(ddlMinsec.SelectedValue)).ToString("yyyy-MM-dd HH:mm:ss.fff");

                }
                else if (skillType.Equals("3"))
                {
                    sc = ((TextBox)item.FindControl("txtCount")).Text;
                }
                else if (skillType.Equals("1"))
                {
                    RadioButtonList raList = item.FindControl("rbtn") as RadioButtonList;
                    sc = raList.SelectedValue;
                }


                TaskScoreInfo socre = new TaskScoreInfo();
                socre.DepartmentID = info.DepartmentID;
                socre.Score = sc;
                socre.SkillID = new Guid(skillID);
                socre.SoldierID = new Guid(info.SoldierID);
                socre.TaskID = new Guid(Request["TaskID"]);
                socre.TaskScoreID = Guid.NewGuid();

                List<TaskScoreInfo> scoreList= TaskScore.GetList(" TaskID='"+Request["TaskID"]+"' And DepartmentID='"+info.DepartmentID+"' And SoldierID='"+info.SoldierID+"' And SkillID='"+skillID+"'");
                if (scoreList == null || scoreList.Count <= 0)
                {
                    TaskScore.Insert(socre);
                }
                else
                {
                    //如果数据库里面存在同一任务同一士兵同一训练技能则只录入一次更新全部
                    foreach (TaskScoreInfo s in scoreList)
                    {
                        socre.TaskScoreID = s.TaskScoreID;
                        socre.DepartmentID = s.DepartmentID;
                        socre.SkillID = s.SkillID;
                        socre.SoldierID = s.SoldierID;
                        socre.TaskID = s.TaskID;
                        socre.Score = sc;
                        TaskScore.Update(socre);
                    }
                }

            }

        }

        btnSave.CommandArgument = "";
	Bind();
    }
    protected void dltSoldiers_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton btn = e.Item.FindControl("lnbtnSoldier") as LinkButton;
        if (btn == null)
            return;
        int count= TaskScore.GetCount(" TaskID='" + Request["TaskID"] + "' And SoldierID='" + btn.CommandArgument + "'");
        if (count > 0)
            btn.ForeColor = Color.Green;
    }
}
